package com.uliky.shouyiLibrary.order.listener;

import com.uliky.common.to.mq.SeckillOrderTo;
import com.uliky.shouyiLibrary.order.entity.OrderEntity;
import com.uliky.shouyiLibrary.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RabbitListener(queues = "order.seckill.order.queue")
public class OrderSeckillListener {

    @Autowired
    OrderService orderService;
    /**
     * 监听死信队列
     */
    @RabbitHandler
    public void  listener(SeckillOrderTo seckillOrder, Message message, Channel channel) throws IOException {
        try {
            orderService.createSeckillOrder(seckillOrder);
            //TODO:手动调用支付宝收单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);

        }

    }
}
